using System;
using System.Data;
using System.Data.Common;
using System.Data.SqlTypes;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BaHuan.Common;
using BaHuan.Entities;
/*
 * For each DB unit, normally tables, we create a DAO class for it
 * this will enalbe us to do some basic tasks for a DB unit
 * such as Save, Delete, GetAll, GetById....
 * 
 * */
namespace BaHuan.DataLayer
{
	
	class HangHoaDAO:BaseDAO
	{
	
		// save into DB, @id = -1: insert else, update
		public ResultObj SaveHangHoa(HangHoa hanghoa)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spAddHangHoa", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spAddHangHoa";
			SqlParameter param = new SqlParameter();
			param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, hanghoa.Id);
			cmd.Parameters.Add(param);
			param = CreateParameter("@MaSo", DbType.String, ParameterDirection.Input, hanghoa.MaSo);
			cmd.Parameters.Add(param);
			param = CreateParameter("@Ten", DbType.String, ParameterDirection.Input, hanghoa.Ten);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DonViTinh", DbType.String, ParameterDirection.Input, hanghoa.DonViTinh);
			cmd.Parameters.Add(param);
			param = CreateParameter("@SoLuong", DbType.Int64, ParameterDirection.Input, hanghoa.SoLuong);
			cmd.Parameters.Add(param);
			param = CreateParameter("@DonGia", DbType.Int64, ParameterDirection.Input, hanghoa.DonGia);
			cmd.Parameters.Add(param);
			param = CreateParameter("@ChuoiTimKiem", DbType.String, ParameterDirection.Input, hanghoa.ChuoiTimKiem);
			cmd.Parameters.Add(param);
			param = CreateParameter("@NoiLuuTru", DbType.String, ParameterDirection.Input, hanghoa.NoiLuuTru);
			cmd.Parameters.Add(param);
			param = CreateParameter("@IdKhayDung", DbType.Int32, ParameterDirection.Input, hanghoa.IdKhayDung);
			cmd.Parameters.Add(param);
            param = CreateParameter("@IsDelete", DbType.Int32, ParameterDirection.Input, hanghoa.IsDelete);
            cmd.Parameters.Add(param);
			param = CreateParameter("@ReturnVal", DbType.String, ParameterDirection.ReturnValue, result.ReturnCode);
			cmd.Parameters.Add(param);
			conObj.Open();
			cmd.ExecuteNonQuery();
			conObj.Close();
			result.ReturnCode = Convert.ToInt32(cmd.Parameters["@ReturnVal"].Value);
			
			return result;
		}
		
		// Get all HangHoa in DB
		public ResultObj GetAllHangHoa( )
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<HangHoa> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllHangHoa", CommandType.StoredProcedure);
			cmd.Connection.Open();
            using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                lst = new List<HangHoa>();
                HangHoa item = null;
                // reader all data from DB one by one
                while (reader.Read())
                {
                    item = new HangHoa();
                    item.Id = Convert.ToInt32(reader["Id"]);
                    item.MaSo = Convert.ToString(reader["MaSo"]);
                    item.Ten = Convert.ToString(reader["Ten"]);
                    item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
                    item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
                    item.DonGia = Convert.ToInt64(reader["DonGia"]);
                    item.ChuoiTimKiem = Convert.ToString(reader["ChuoiTimKiem"]);
                    item.NoiLuuTru = Convert.ToString(reader["NoiLuuTru"]);
                    item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
                    item.IdKhayDung = reader["IdKhayDung"] == System.DBNull.Value ? -1 : Convert.ToInt32(reader["IdKhayDung"]);
                    lst.Add(item);
                }
            }
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get all HangHoa in DB with condition
		public ResultObj GetAllHangHoaByID(HangHoa hanghoa)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<HangHoa> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetAllHangHoaByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, hanghoa.Id);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<HangHoa>();
			HangHoa item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new HangHoa();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.MaSo = Convert.ToString(reader["MaSo"]);
			item.Ten = Convert.ToString(reader["Ten"]);
			item.DonViTinh = Convert.ToString(reader["DonViTinh"]);
			item.SoLuong = Convert.ToInt64(reader["SoLuong"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
			item.ChuoiTimKiem = Convert.ToString(reader["ChuoiTimKiem"]);
			item.NoiLuuTru = Convert.ToString(reader["NoiLuuTru"]);
			item.IdKhayDung = Convert.ToInt32(reader["IdKhayDung"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Delete a HangHoa out of DB with condition
		public ResultObj DeleteHangHoa(HangHoa hanghoa)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			SqlConnection conObj = DataConnection.GetSqlConnection();
			SqlCommand cmd = CreateSqlCommand("spDeleteHangHoa", CommandType.StoredProcedure);
			cmd.CommandType = CommandType.StoredProcedure;
			cmd.CommandText = "spDeleteHangHoa";
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, hanghoa.Id);
			cmd.Parameters.Add(param);
			conObj.Open();
			result.ReturnCode = cmd.ExecuteNonQuery();
			conObj.Close();
			
			return result;
		}
		
		// Get info of DonGiaNguyenVatLieu from DB with condition
		public ResultObj GetDonGiaNguyenVatLieu(int IdDonGiaNguyenVatLieu)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<DonGiaNguyenVatLieu> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetDonGiaNguyenVatLieuByID", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdDonGiaNguyenVatLieu);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<DonGiaNguyenVatLieu>();
			DonGiaNguyenVatLieu item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new DonGiaNguyenVatLieu();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.IdHangHoa = Convert.ToInt32(reader["IdHangHoa"]);
			item.IdNhaCungCap = Convert.ToInt32(reader["IdNhaCungCap"]);
			item.NgayThang = Convert.ToDateTime(reader["NgayThang"]);
			item.DonGia = Convert.ToInt64(reader["DonGia"]);
            item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
 
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
		
		// Get info of KhayDung from DB with condition
		public ResultObj GetKhayDung(int IdKhayDung)
        		{
			ResultObj result = new ResultObj();
			result.Result = null;
			List<KhayDung> lst = null;
			SqlCommand cmd = CreateSqlCommand("spGetKhayDung", CommandType.StoredProcedure);
			SqlParameter param = CreateParameter("@Id", DbType.Int32, ParameterDirection.Input, IdKhayDung);
			cmd.Parameters.Add(param);
			cmd.Connection.Open();
			using (SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
			{
			lst = new List<KhayDung>();
			KhayDung item = null;
			// reader all data from DB one by one
			while (reader.Read())
			{
			item = new KhayDung();
			item.Id = Convert.ToInt32(reader["Id"]);
			item.Ten = Convert.ToString(reader["Ten"]);
			item.SoLuongHop = Convert.ToInt64(reader["SoLuongHop"]);
			item.SoLuongTrung = Convert.ToInt64(reader["SoLuongTrung"]);
            //item.IsDelete = Convert.ToInt32(reader["IsDelete"]);
			lst.Add(item);
			}
			}
			result.Result = lst;
			result.ReturnCode = 1;
			
			return result;
		}
					
	}
}
